package com.ebupt.migu.music.userGroup.controller;

import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.system.entity.SysUser;
import com.ebupt.migu.music.userGroup.entity.UserGroupInfo;
import com.ebupt.migu.music.userGroup.entity.dto.UserGroupDTO;
import com.ebupt.migu.music.userGroup.entity.vo.UserGroupVo;
import com.ebupt.migu.music.userGroup.service.CrowdListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Intellij IDEA.
 * User:  liuyangyang@cd.ebupt.com
 * Date:  2019/9/13
 *
 * @author liuyangyang
 */
@RestController
@RequestMapping("/crowd")
public class CrowdListController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private CrowdListService crowdListService;
    @Autowired
    private IDAuthUtils idAuthUtils;
    @Resource
    private AuthBaseUtils authBaseUtils;
    @Resource
    private ReqestParamUtil reqestParamUtil;

    /**
     * 条件、分页查询人群列表
     *
     * @return
     */
    @RequestMapping("/list")
    public ResultObject crowdList() {
        // 获取用户登录信息
        SysUser user = authBaseUtils.getUserByToken(request);
        Map<String, Object> paramsWithNoUserInfo = ReqestParamUtil.getParams(request);
        Map<String, Object> params = reqestParamUtil.getParams2(request);
        params = idAuthUtils.isAdmin(params);
        // 增加判断全量角色
        params = idAuthUtils.isFullAmountGroupsAdmin(params);
        paramsWithNoUserInfo.put("isAdmin", params.get("isAdmin"));
        paramsWithNoUserInfo.put("isFAGAdmin", params.get("isFAGAdmin"));
        return crowdListService.crowdList(paramsWithNoUserInfo, user);
    }

    /**
     * 【创建策略专供】
     * 条件、分页查询人群列表
     *
     * @return
     */
    @RequestMapping("/lite_list")
    public ResultObject liteList() {
        // 获取用户登录信息
        SysUser user = AuthBaseUtils.getSessionUser(request);
        Map<String, Object> paramsWithNoUserInfo = ReqestParamUtil.getParams(request);
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        // 增加判断全量角色
        params = idAuthUtils.isFullAmountGroupsAdmin(params);
        paramsWithNoUserInfo.put("isAdmin", params.get("isAdmin"));
        paramsWithNoUserInfo.put("isFAGAdmin", params.get("isFAGAdmin"));
        return crowdListService.liteList(paramsWithNoUserInfo, user);
    }

    /**
     * 手动更新人群
     * 1. 验证发起请求的用户是否是创建人或者超级管理员
     *
     * @return
     */
    @RequestMapping("/refresh")
    public ResultObject refreshCrowd() {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdminByToken(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权刷新此人群");
        }
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return crowdListService.refreshCrowd(params);
    }

    /**
     * 更新人群信息
     * 1. 验证发起请求的用户是否是创建人或者超级管理员
     *
     * @return
     */
    @RequestMapping("/update")
    public ResultObject updateCrowd() {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdminByToken(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权刷新此人群");
        }
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return crowdListService.updateCrowd(params);
    }

    /**
     * 删除人群
     * 1. 验证发起请求的用户是否是创建人或者超级管理员
     *
     * @return
     */
    @RequestMapping("/delete")
    public ResultObject deleteCrowd() {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdminByToken(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权删除此人群");
        }
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return crowdListService.deleteCrowd(params);
    }

    /**
     * 创建人群
     *
     * @return
     */
    @RequestMapping("/add")
    public ResultObject addCrowd() {
        Map<String, Object> params = reqestParamUtil.getParams2(request);
        return crowdListService.addCrowd(params);
    }

    /**
     * 查询人群是否可以被修改或删除
     * 1、 被重组的人群不饿可以被修改或删除
     * 2、 当人群被策略引用，仅策略状态为已结束时，该人群可以被修改或删除
     *
     * @return
     */
    @RequestMapping("/delete_or_update")
    public ResultObject deleteORupdate() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        return crowdListService.deleteORupdate(params);
    }

    /**
     * 通过人群名称查询人群详情
     *
     * @return
     */
    @RequestMapping("/queryGroupDetailByName")
    public ResultObject queryGroupDetailByName() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return crowdListService.queryGroupDetailByName(params);
    }

    /**
     * 【变色龙弹窗查询专供】
     * <br>
     * 查询人群列表
     *
     * @return 查询结果
     */
    @RequestMapping("/queryUserGroup")
    public ResultObject<List<Map<String, Object>>> queryUserGroup() {
        // 获取请求参数并封装到map中
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        // 判断用户是否管理员
        params = idAuthUtils.isAdmin(params);
        return crowdListService.queryUserGroup(params);
    }

    /**
     * <p>【客群洞察】</p>
     * 查询手机号有效的服务云侧创建人群
     *
     * @return 人群
     */
    @PostMapping("/queryUserGroupForInsight")
    public ResultObject<List<UserGroupInfo>> queryUserGroupForInsight() {
        return crowdListService.queryUserGroupForInsight();
    }

    /**
     * <p>【客群洞察】</p>
     * 将redis中的人群信息写入到本地文件中
     *
     * @param map 包含人群id的集合
     * @return 操作结果
     */
    @PostMapping("/downloadUserGroup")
    public ResultObject<String> downloadUserGroup(@RequestBody Map<String, String> map) {
        return crowdListService.downloadUserGroup(map.get("groupId"));
    }


    /**
     * 变色龙 查询人群列表 查询条件 （创建方式 人群状态）
     *
     * @return
     */
    @PostMapping("/queryUserGroupList")
    public ResultObject<List<UserGroupVo>> queryUserGroupList(@RequestBody UserGroupDTO userGroupDTO) {
        // 获取用户登录信息
        SysUser user = authBaseUtils.getUserByToken(request);
        return crowdListService.queryUserGroupList(userGroupDTO, user);
    }


}
